import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule } from '@nestjs/config';
import { APP_GUARD } from '@nestjs/core';
import { validationSchema } from './config/validation-schema';
import configuration from './config/configuration';
import { dataSourceOptions } from './config/data-source';
import jwtConfig from './config/jwt.config';
import { AuthModule } from './modules/auth/auth.module';
import { WorksModule } from './modules/works/works.module';
import { CommentsModule } from './modules/comments/comments.module';
import { FriendsModule } from './modules/friends/friends.module';
import { MessagesModule } from './modules/messages/messages.module';
import { NotificationsModule } from './modules/notifications/notifications.module';
import { UploadModule } from './modules/upload/upload.module';
import { AuthGuard } from './common/guards/auth.guard';

@Module({
  imports: [
    // 配置模块：根据环境加载不同的配置文件
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: `.env.${process.env.NODE_ENV || 'development'}`,
      ignoreEnvFile: process.env.NODE_ENV === 'production', // 生产环境使用系统环境变量
      load: [configuration],
      validationSchema,
      validationOptions: {
        allowUnknown: true,
        abortEarly: false,
      },
      expandVariables: true,
    }),

    // 数据库模块：直接使用配置
    TypeOrmModule.forRoot(dataSourceOptions),

    // JWT模块：全局可用
    jwtConfig,

    // 业务模块
    AuthModule,
    WorksModule,
    CommentsModule,
    FriendsModule,
    MessagesModule,
    NotificationsModule, // WebSocket 通知模块
    UploadModule, // 文件上传模块
  ],
  controllers: [],
  providers: [
    // 全局启用 JWT 认证守卫
    {
      provide: APP_GUARD,
      useClass: AuthGuard,
    },
  ],
})
export class AppModule { }
